#Region " Imports "

Imports System.Data
Imports BlazeApps.Library.Options

#End Region

Partial Class Usercontrols_Options_Options
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "

    Public Property ItemGuid() As Guid
        Get
            Return GetGuidFromVS("ItemGuid")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("ItemGuid", value)
        End Set
    End Property

    Public Property SetInitialOptions() As Boolean
        Get
            Return GetBooleanFromVS("IsInitialOptions")
        End Get
        Set(ByVal value As Boolean)
            ViewState.Add("IsInitialOptions", value)
        End Set
    End Property

    Protected Property OptionGuid() As Guid
        Get
            Return GetGuidFromVS("OptionID")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("OptionID", value)
        End Set
    End Property

#End Region

#Region " Page Events "

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If ItemGuid = Guid.Empty Then
            GetQueryStringValues()
        End If

        If Me.Page.IsPostBack = False Then
            LoadOptions()
        End If

    End Sub

#End Region

#Region " Save Data "

    Public Sub SaveOptions()

        Dim Item As OptionDetail = OptionDetail.NewOptions()

        If SetInitialOptions() Then

            Item.ItemID = ItemGuid
            Item.AllowSummary = True
            Item.AllowComments = True
            Item.AllowRanking = True
            Item.AllowTags = True
            Item.AllowInfoTools = True
            Item.AllowSubscriptions = True

        Else

            Item.ItemID = ItemGuid
            Item.AllowSummary = Me.uxAllowSummaryCheckbox.Checked
            Item.AllowComments = Me.uxAllowCommentsCheckbox.Checked
            Item.AllowRanking = Me.uxAllowRankingCheckbox.Checked
            Item.AllowTags = Me.uxAllowTagsCheckbox.Checked
            Item.AllowInfoTools = Me.uxAllowToolsCheckbox.Checked
            Item.AllowSubscriptions = Me.uxAllowFavoritesCheckbox.Checked

        End If

        Item.Save()

    End Sub

    Private Sub UpdateOptions()

        Dim Item As OptionDetail = OptionDetail.GetOptions(ItemGuid)

        Item.ItemID = ItemGuid
        Item.AllowSummary = Me.uxAllowSummaryCheckbox.Checked
        Item.AllowComments = Me.uxAllowCommentsCheckbox.Checked
        Item.AllowRanking = Me.uxAllowRankingCheckbox.Checked
        Item.AllowTags = Me.uxAllowTagsCheckbox.Checked
        Item.AllowInfoTools = Me.uxAllowToolsCheckbox.Checked
        Item.AllowSubscriptions = Me.uxAllowFavoritesCheckbox.Checked

        Item.Save()

    End Sub

#End Region

#Region " Load Data "

    Public Sub LoadOptions()
        If Not (ItemGuid = Guid.Empty) Then
            Dim Items As OptionDetail = OptionDetail.GetOptions(ItemGuid)

            If Items.hasdata Then
                Me.uxAllowSummaryCheckbox.Checked = Items.AllowSummary
                Me.uxAllowCommentsCheckbox.Checked = Items.AllowComments
                Me.uxAllowRankingCheckbox.Checked = Items.AllowRanking
                Me.uxAllowTagsCheckbox.Checked = Items.AllowTags
                Me.uxAllowToolsCheckbox.Checked = Items.AllowInfoTools
                Me.uxAllowFavoritesCheckbox.Checked = Items.AllowSubscriptions

                OptionGuid = Items.OptionsID
            End If

        End If
    End Sub

#End Region

#Region " Form Controls Events "

    Protected Sub uxSaveButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxSaveButton.Click

        If OptionGuid = Guid.Empty Then
            SaveOptions()
        Else
            UpdateOptions()
        End If

    End Sub

#End Region

#Region " Helpers and Methods "

    Protected Sub GetQueryStringValues()
        ItemGuid = GetGuidFromQueryString("id")
    End Sub

#End Region

End Class
